//
//  PlanAddView.m
//  SportSocial
//
//  Created by yull on 15/2/4.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "PlanAddView.h"

@implementation PlanAddView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.imageArray = @[@"basketball_icon_normal",@"football_icon_normal",@"running_icon_normal",@"swimming_icon_normal",@"billiards_icon_normal",@"tennis_icon_normal",@"table_icon_normal"];
        self.selectImageArray = @[@"basketball_icon_pressed",@"football_icon_pressed",@"running_icon_pressed",@"swimming_icon_pressed",@"billiards_icon_pressed",@"tennis_icon_pressed",@"table_icon_pressed"];
        self.titleArray = @[@"篮球",@"足球",@"跑步",@"游泳",@"台球",@"网球",@"乒乓球"];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
        label.backgroundColor = [UIColor clearColor];
        label.text = @"  添加新运动";
        label.textColor = RGB(195, 140, 141);
        [self addSubview:label];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, self.width, 1)];
        lineView.backgroundColor = RGB(217, 235, 209);
        [label addSubview:lineView];
        
        [self addButton];
    }
    return self;
}

- (void)addButton
{
    CGFloat totalHeight = 40;
    
    for (NSInteger i = 0;i<self.imageArray.count;i++) {
        CGFloat spacing = 30;
        CGFloat width = (self.width - 40 - 3*spacing)/4;
        
        CGRect rect = CGRectMake(20 + i % 4 * (width + spacing), 10 + 40 + i/4 * (width + 10), width, width);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = rect;
        button.tag = 10 + i;
        [button setImage:[UIImage imageNamed:[self.imageArray objectAtIndex:i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[self.selectImageArray objectAtIndex:i]] forState:UIControlStateHighlighted];
        [self addSubview:button];
        
        [button addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        totalHeight = button.bottom + 10;
    }
    
    self.height = totalHeight;
}

- (void)onClickButton:(UIButton *)sender
{
    NSString *name = [self.titleArray objectAtIndex:(sender.tag -10)];
    NSString *imageName = [self.imageArray objectAtIndex:(sender.tag - 10)];
    
    if ([self.delegate respondsToSelector:@selector(selectSportName:imageName:)]) {
        [self.delegate selectSportName:name imageName:imageName];
    }
}
@end
